package com.chenglu.android.mine.me;

import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.bumptech.glide.Glide;
import com.chenglu.android.R;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BaseMvpFragment;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.AppCheckInstall;
import com.chenglu.android.common.utils.BaseDialog;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.mine.address.AddressManagerActivity;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.bean.UserBalanceBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.beans.BeansActivity;
import com.chenglu.android.mine.collect.CollectActivity;
import com.chenglu.android.mine.coupon.CouponActivity;
import com.chenglu.android.mine.login.LoginActivity;
import com.chenglu.android.mine.me.mvp.contract.MineContract;
import com.chenglu.android.mine.me.mvp.presenter.MinePresenter;
import com.chenglu.android.mine.member.MemberCenterActivity;
import com.chenglu.android.mine.platform.PlatformActivity;
import com.chenglu.android.mine.setting.SettingActivity;
import com.chenglu.android.mine.wallet.WalletActivity;
import com.chenglu.android.onelogin.OneKeyLoginActivity;
import com.chenglu.android.order.OrderActivity;
import com.gyf.immersionbar.ImmersionBar;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023 3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class MineFragment extends BaseMvpFragment<MinePresenter> implements MineContract.View {


    @BindView(R.id.iv_user)
    ImageView ivUser;
    @BindView(R.id.tv_user_tologin)
    TextView tvUserTologin;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_level)
    TextView tvUserLevel;
    @BindView(R.id.tv_user_service_center)
    TextView tvUserServiceCenter;
    @BindView(R.id.ll_user_login)
    LinearLayout llUserLogin;
    @BindView(R.id.ll_user_collect)
    LinearLayout llUserCollect;
    @BindView(R.id.ll_user_order_list)
    LinearLayout llUserOrderList;
    @BindView(R.id.ll_user_custom)
    LinearLayout llUserCustom;
    @BindView(R.id.ll_user_message)
    LinearLayout llUserMessage;
    @BindView(R.id.tv_user_share)
    TextView tvUserShare;
    @BindView(R.id.rl_user_member_center)
    RelativeLayout rlUserMemberCenter;
    @BindView(R.id.tv_user_wallet)
    TextView tvUserWallet;
    @BindView(R.id.rl_sy_app)
    RelativeLayout rlSYapp;
    @BindView(R.id.tv_user_beans)
    TextView tvUserBeans;
    @BindView(R.id.ll_user_beans)
    LinearLayout llUserBeans;
    @BindView(R.id.tv_user_money)
    TextView tvUserMoney;
    @BindView(R.id.ll_mine_money)
    LinearLayout llMineMoney;
    @BindView(R.id.tv_user_point)
    TextView tvUserPoint;
    @BindView(R.id.ll_user_point)
    LinearLayout llUserPoint;
    @BindView(R.id.tv_user_cards)
    TextView tvUserCards;
    @BindView(R.id.ll_user_cards)
    LinearLayout llUserCards;
    @BindView(R.id.tv_order_center)
    TextView tvOrderCenter;
    @BindView(R.id.rt_order_center)
    RelativeLayout rtOrderCenter;
    @BindView(R.id.iv1)
    ImageView iv1;
    @BindView(R.id.rl_setting_message)
    RelativeLayout rlSettingMessage;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.rl_address_manage)
    RelativeLayout rlAddressManage;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.rl_zizhi)
    RelativeLayout rlZizhi;
    @BindView(R.id.iv4)
    ImageView iv4;
    @BindView(R.id.rl_user_exit)
    RelativeLayout rlUserExit;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.toolbar_username)
    TextView toolbarUsername;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fl_center)
    FrameLayout flCenter;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.fl_level)
    FrameLayout flLevel;

    public static MineFragment newInstance() {
        MineFragment mineFragment = new MineFragment();
        return mineFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    //    private int mOffset = 0;
    private int mScrollY = 0;

    private String aaccessToken;

    OnConfirmListener onConfirmListener;

    @Override
    protected void initView() {
        initScroolview();
        mPresenter = new MinePresenter();
        mPresenter.attachView(this);


    }


    @Override
    public void onResume() {
        super.onResume();
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .autoDarkModeEnable(true)
                .fullScreen(true)
                .statusBarDarkFont(true)
                .init();
        if (isLogin) {
            tvLogin.setText("退出登录");
            LogUtils.e("islogin=" + isLogin);
            aaccessToken = SPUtils.getString(Constant.AaccessToken, null);
            mPresenter.getUserInfo(aaccessToken);
            mPresenter.getUserBalance(aaccessToken);
        } else {
            tvLogin.setText("账号登录");
            LogUtils.e("未登录");
            setValues();

        }


    }


    //初始化滑动显示和隐藏toolbar
    private void initScroolview() {


        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = Global.dp2px(60);
            int color = ContextCompat.getColor(getContext(), R.color.white) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
//                    buttonBarLayout.setAlpha(1f * mScrollY / h);
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
//                    ivHeader.setTranslationY(mOffset - mScrollY);
                }
                if (scrollY == 0) {
                    toolbarUsername.setTextColor(getResources().getColor(R.color.transparent));

                } else {
                    toolbarUsername.setTextColor(getResources().getColor(R.color.all_black_text));


                }

                lastScrollY = scrollY;
            }
        });
        toolbar.setBackgroundColor(0);


    }

    BasePopupView basePopupView;

    @OnClick({R.id.rl_setting_message, R.id.rl_address_manage, R.id.rl_zizhi, R.id.rl_user_exit, R.id.rl_sy_app, R.id.ll_user_beans, R.id.ll_mine_money,
            R.id.ll_user_collect, R.id.rl_user_member_center, R.id.ll_user_order_all, R.id.ll_user_order_waitpay, R.id.ll_user_order_illustrate
            , R.id.ll_user_order_comment, R.id.ll_user_cards, R.id.cv_user, R.id.tv_user_tologin, R.id.ll_user_order_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_user_order_list:
                if (isLogin) {
                    startActivity(new Intent(getActivity(), OrderActivity.class)
                            .putExtra(OrderActivity.STATUS, "0"));
                } else {
                    login();
                }
                break;
            case R.id.cv_user:
                if (isLogin) {

                } else {
                    login();
                }
                break;
            case R.id.tv_user_tologin:

                login();

                break;

            case R.id.rl_address_manage:
                if (isLogin) {
                    startActivity(new Intent(getActivity(), AddressManagerActivity.class));
                } else {
                    login();
                }
                break;
            case R.id.rl_setting_message:
                if (isLogin) {
                    startActivity(new Intent(getActivity(), SettingActivity.class));
                } else {
                    login();
                }

                break;
            case R.id.rl_zizhi:
                //平台资质
                startActivity(new Intent(getActivity(), PlatformActivity.class));
                break;
            case R.id.rl_user_exit:
                //退出的接口
                if (isLogin) {
//                    BaseDialog.showDialog(getActivity(), "确认退出登录", "取消", "确认", new OnConfirmListener() {
//                        @Override
//                        public void onConfirm() {
                    mPresenter.getExitLogin(aaccessToken);
//                        }
//                    },false);

                } else {
                    login();
                }


                break;
            case R.id.rl_sy_app:

                BaseDialog.showDialog(getActivity(), getActivity().getResources().getString(R.string.start_sy_app),
                        "取消", "确认", new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                if (AppCheckInstall.isSYAvilible(getActivity())) {

                                    Intent intent = new Intent();
                                    ComponentName toActivity = new ComponentName("com.symall.android", "com.symall.android.common.luanch.LaunchActivity");
                                    try {
                                        intent.setComponent(toActivity);
                                        startActivity(intent);
                                        getActivity().finish();
//                    System.exit(0);
                                    } catch (Exception e) {
                                        LogUtils.e(e.getMessage());
                                    }
                                } else {
                                    ToastUtils.show("请安装SY商城APP");
                                }
                            }
                        }, new OnCancelListener() {
                            @Override
                            public void onCancel() {

                            }
                        }, false);
                break;

            case R.id.ll_user_beans:
                if (isLogin) {
                    getActivity().startActivity(new Intent(getActivity(), BeansActivity.class));
                } else {
                    login();
                }
                break;
            case R.id.ll_mine_money:
                if (isLogin) {
                    getActivity().startActivity(new Intent(getActivity(), WalletActivity.class));
                } else {
                    login();
                }

                break;
            case R.id.ll_user_collect:
                if (isLogin) {
                    getActivity().startActivity(new Intent(getActivity(), CollectActivity.class));
                } else {
                    login();
                }

                break;
            //会员中心
            case R.id.rl_user_member_center:
                if (isLogin) {
                    getActivity().startActivity(new Intent(getActivity(), MemberCenterActivity.class)
                            .putExtra(MemberCenterActivity.HEADPHOTO, userInfoBean.getData().getAvatar())
                            .putExtra(MemberCenterActivity.NICKNAME, userInfoBean.getData().getNick_name()));
                } else {
                    login();
                }

                break;
            case R.id.ll_user_order_all:
                //全部订单
                if (isLogin) {
                    startActivity(new Intent(getActivity(), OrderActivity.class)
                            .putExtra(OrderActivity.STATUS, "0"));
                } else {
                    login();
                }
                break;
            case R.id.ll_user_order_waitpay:
                //待付款
                if (isLogin) {
                    startActivity(new Intent(getActivity(), OrderActivity.class)
                            .putExtra(OrderActivity.STATUS, "1"));
                } else {
                    login();
                }
                break;
            case R.id.ll_user_order_illustrate:
                //待说明
                if (isLogin) {
                    startActivity(new Intent(getActivity(), OrderActivity.class)
                            .putExtra(OrderActivity.STATUS, "2"));
                } else {
                    login();
                }
                break;
            case R.id.ll_user_order_comment:
                //待评论
                if (isLogin) {
                    startActivity(new Intent(getActivity(), OrderActivity.class)
                            .putExtra(OrderActivity.STATUS, "3"));
                } else {
                    login();
                }
                break;
            //卡卷
            case R.id.ll_user_cards:

                if (isLogin) {
                    startActivity(new Intent(getActivity(), CouponActivity.class));
                } else {
                    login();
                }
                break;


        }

    }

    private UserInfoBean userInfoBean;

    //所有的信息接口
    @Override
    public void getUserInfo(UserInfoBean bean) {
        if (bean.getSuccess()) {
            userInfoBean = bean;
            flLevel.setVisibility(View.VISIBLE);
            llUserLogin.setVisibility(View.VISIBLE);
            tvUserTologin.setVisibility(View.GONE);
            GlideUtils.setImageView(mContext, bean.getData().getAvatar(), ivUser, 15);
//            switch (bean.getData().getLevel()) {
//                case 0:
//                    tvUserLevel.setText(bean.getData().getLevelName());
//                    break;
//                case 1:
//                    tvUserLevel.setText("一星用户");
//                    break;
//                case 2:
//                    tvUserLevel.setText("二星用户");
//                    break;
//                case 3:
//                    tvUserLevel.setText("三星用户");
//                    break;
//                case 4:
//                    tvUserLevel.setText("四星用户");
//                    break;
//                case 5:
//                    tvUserLevel.setText("五星用户");
//                    break;
//
//            }
            if (bean.getData().getIsServiceCenter() == 1) {
                flCenter.setVisibility(View.VISIBLE);
                tvUserServiceCenter.setText("服务中心");
            } else {
                flCenter.setVisibility(View.GONE);
            }

            //服务中心
            if (bean.getData().getIsServiceCenter() == 1) {
                tvUserServiceCenter.setVisibility(View.VISIBLE);
            } else {
                tvUserServiceCenter.setVisibility(View.GONE);
            }
            tvUserLevel.setText(bean.getData().getLevelName());//会员名称
            tvUserName.setText(bean.getData().getNickName());
            tvUserPoint.setText(Utils.formatZeroNumber(bean.getData().getIntegral())); //积分


        } else {
            llUserLogin.setVisibility(View.GONE);
            tvUserTologin.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(ContextCompat.getDrawable(mContext, R.drawable.mine_icon_head_photo)).into(ivUser);


        }
    }


    //卡卷 余额
    @Override
    public void getUserBalance(UserBalanceBean bean) {
        if (bean.getSuccess()) {
            tvUserBeans.setText(Utils.formatZeroNumber(bean.getData().getBeans()));
            tvUserMoney.setText(Utils.formatZeroNumber(bean.getData().getMoney()));
            tvUserCards.setText(Utils.formatZeroNumber(bean.getData().getCouponCount()));
        } else {
            tvUserBeans.setText("0");
            tvUserPoint.setText("0"); //积分
            tvUserMoney.setText("0");
            tvUserCards.setText("0");
        }
    }


    @Override
    public void getExitLogin(SmsCodeBean bean) {
        if (bean.getCode() == Constant.HTTPCODE200) {
            isLogin = false;
            SPUtils.setBoolean("ISLOGIN", false);

            LogUtils.e("Utillscad" + Utils.checkSim(mContext));
            login();

        } else {
            ToastUtils.show(bean.getMsg());
        }

    }

    //跳转登录的方法
//    private void login() {
//
//        if (Utils.checkSim(mContext)) {
//            //判断是否是模拟器
//            if (Utils.isEmulator()) {
//                startActivity(new Intent(getActivity(), LoginActivity.class));
//            } else {
//                startActivity(new Intent(getActivity(), OneKeyLoginActivity.class));
//            }
//
//        } else {
//            startActivity(new Intent(getActivity(), LoginActivity.class));
//        }
//    }

    //平台资质
    @Override
    public void getPlatformPics(ReturnStringBean bean) {

    }


    //获取失败
    @Override
    public void onError(String errMessage) {
        super.onError(errMessage);
        setValues();
    }


    private void setValues() {
        llUserLogin.setVisibility(View.GONE);
        tvUserTologin.setVisibility(View.VISIBLE);
        Glide.with(mContext).load(ContextCompat.getDrawable(mContext, R.drawable.mine_icon_head_photo)).into(ivUser);
        tvUserPoint.setText("0"); //积分
        tvUserBeans.setText("0");//红豆
        tvUserMoney.setText("0");  //余额
        tvUserCards.setText("0"); //卡券
    }


    //红豆 积分余额的请求错误接口
    @Override
    public void onUserBalanceError(String errMessage) {

        tvUserPoint.setText("0"); //积分
        tvUserBeans.setText("0");//红豆
        tvUserMoney.setText("0");  //余额
        tvUserCards.setText("0"); //卡券
    }


}
